package com.web3.management.config;

import com.web3.management.security.AccountClientTokenFilter;
import com.web3.management.security.CustomUserDetailsService;
import com.web3.management.security.JwtAccessDeniedHandler;
import com.web3.management.security.JwtAuthenticationEntryPoint;
import com.web3.management.security.JwtAuthenticationFilter;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private final CustomUserDetailsService userDetailsService;
    private final JwtAuthenticationEntryPoint authenticationEntryPoint;
    private final JwtAccessDeniedHandler accessDeniedHandler;
    private final JwtAuthenticationFilter jwtAuthenticationFilter;
    private final AccountClientTokenFilter accountClientTokenFilter;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .exceptionHandling()
                .authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(accessDeniedHandler)
                .and()
                .authorizeRequests()
                .antMatchers(HttpMethod.POST, "/api/auth/login").permitAll()
                .antMatchers(HttpMethod.POST, "/api/auth/refresh").permitAll()
                .antMatchers("/api/auth/logout", "/api/auth/verify").authenticated()
                .antMatchers("/actuator/health", "/actuator/info", "/actuator/metrics").permitAll()
                .antMatchers("/error", "/", "/doc.html", "/swagger-ui/**", "/v3/api-docs/**").permitAll()
                .antMatchers(HttpMethod.GET, "/api/accounts/*/effective-config").hasAnyRole("ADMIN", "ACCOUNT_CLIENT")
                .antMatchers(HttpMethod.GET, "/api/accounts/*").hasAnyRole("ADMIN", "ACCOUNT_CLIENT")
                .antMatchers(HttpMethod.POST, "/api/trade-logs/**").hasAnyRole("ADMIN", "ACCOUNT_CLIENT")
                .antMatchers(HttpMethod.GET, "/api/system/settings/random-sleep").hasAnyRole("ADMIN", "ACCOUNT_CLIENT")
                .antMatchers(HttpMethod.GET, "/api/system/settings").hasAnyRole("ADMIN", "ACCOUNT_CLIENT")
                .antMatchers("/api/client/**").hasRole("ACCOUNT_CLIENT")
                .antMatchers("/tm/**").permitAll()
                .antMatchers("/api/**").hasRole("ADMIN")
                .anyRequest().authenticated()
                .and()
                .addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
                .addFilterBefore(accountClientTokenFilter, JwtAuthenticationFilter.class);
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}
